package com.health.work.utils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;


public class AsyncImageLoader {
	//异步加载图片
	private HashMap<String, SoftReference<Drawable>> imageCache;
	Drawable drawable;
	private String filePath;

	public AsyncImageLoader() {
		imageCache = new HashMap<String, SoftReference<Drawable>>();
	}
	
	public AsyncImageLoader(String filePath) {
		imageCache = new HashMap<String, SoftReference<Drawable>>();
		this.filePath = filePath;
	}

	public Drawable loadDrawable(final String imageUrl,
			final ImageCallback imageCallback,final String size) {
		if (imageCache.containsKey(imageUrl)) {
			SoftReference<Drawable> softReference = imageCache.get(imageUrl);
			if(drawable!=null){
				drawable=null;
			}
			drawable = softReference.get();
			if (drawable != null) {
				return drawable;
			}
		}
		final Handler handler = new Handler() {
			public void handleMessage(Message message) {
				imageCallback.imageLoaded((Drawable) message.obj, imageUrl,size);
			}
		};
		new Thread() {
			@Override
			public void run() {
				if(imageUrl!=null&&!"".equals(imageUrl)){
				    if(drawable!=null){
				    	drawable=null;
				    }
					drawable = loadImageFromUrl(imageUrl);
					if(drawable==null)
						return;
					String fileName = new Tools().getFileName(imageUrl);
					if(drawable!=null && filePath!=null && !filePath.equals("")) {
						new Tools().saveImageToSD(((BitmapDrawable) drawable).getBitmap(), filePath, fileName);
					}
					imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
					Message message = handler.obtainMessage(0, drawable);
					handler.sendMessage(message);
				}
				
			}
		}.start();
		return null;
	}
	public Drawable loadDrawable(final String imageUrl,
			final ImageCallback imageCallback) {
		if (imageCache.containsKey(imageUrl)) {
			SoftReference<Drawable> softReference = imageCache.get(imageUrl);
			Drawable drawable = softReference.get();
			if (drawable != null) {
				return drawable;
			}
		}
		final Handler handler = new Handler() {
			public void handleMessage(Message message) {
				imageCallback.imageLoaded((Drawable) message.obj, imageUrl, imageUrl);
			}
		};
		new Thread() {
			@Override
			public void run() {
				if(drawable!=null){
					drawable=null;
				}
				drawable = loadImageFromUrl(imageUrl);
				if(drawable==null)
					return;
				String fileName = new Tools().getFileName(imageUrl);
				if(drawable!=null && filePath!=null && !filePath.equals("")) {
					new Tools().saveImageToSD(((BitmapDrawable) drawable).getBitmap(), filePath, fileName);
				}
				imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
				Message message = handler.obtainMessage(0, drawable);
				handler.sendMessage(message);
			}
		}.start();
		return null;
	}

	public  Drawable loadImageFromUrl(String url) {
		URL m;
		Drawable d = null;
		InputStream i = null;
			try {
				m = new URL(url);
				if(m==null||m.openStream()==null){
					return null;
				}
				i = (InputStream) m.openStream();
			
			} catch (Exception e) {
				e.printStackTrace();
				if(i!=null) {
					try {
						i.close();
					} catch (IOException e1) {
						e1.printStackTrace();
						return null;
					}
				}
				return null;
			}
			try {
				d = Drawable.createFromStream(i, "src");
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			
			if(i!=null) {
				try {
					i.close();
				} catch (IOException e) {
					e.printStackTrace();
					return null;
				}
			}
			return d;
	}

	public interface ImageCallback {
		public void imageLoaded(Drawable imageDrawable, String imageUrl,String tag);
	}

}